package com.app.reportproblem;

import org.json.JSONException;
import org.json.JSONObject;

import com.app.reportproblem.MyUI.KeyboardHelper;
import com.app.reportproblem.MyUI.MyClickAbleImageView;
import com.app.reportproblem.Utils.GPSTracker;
import com.app.reportproblem.Utils.PreferenceHelper;
import com.app.reportproblem.httpclient.HttpClientHelper;
import com.app.reportproblem.httpclient.MyJsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener,
		LocationListener {

	Button btnReport, btnView;
	MyClickAbleImageView btnBrokenStreetLight, btnWaterLeak, btnPothole,
			btnGarbage, btnPowerLine, btnRoadObstruction,
			btnBrokenTrafficLight;
	boolean isChoose = true;

	PreferenceHelper preferenceHelper;
	String title;

	GPSTracker gpsTracker;

	double mLatitude;
	double mLongitude;

	ImageView actionbar;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565).build();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		preferenceHelper = new PreferenceHelper(this);

		btnReport = (Button) findViewById(R.id.btn_report);
		btnView = (Button) findViewById(R.id.btn_view_report);
		btnReport.setSelected(true);

		actionbar = (ImageView) findViewById(R.id.actionbar);

		btnBrokenStreetLight = (MyClickAbleImageView) findViewById(R.id.btn_broken_street_light);
		btnWaterLeak = (MyClickAbleImageView) findViewById(R.id.btn_water_leak);
		btnPothole = (MyClickAbleImageView) findViewById(R.id.btn_pothole);
		btnGarbage = (MyClickAbleImageView) findViewById(R.id.btn_garbage);
		btnPowerLine = (MyClickAbleImageView) findViewById(R.id.btn_power_line);
		btnRoadObstruction = (MyClickAbleImageView) findViewById(R.id.btn_road_obstruction);
		btnBrokenTrafficLight = (MyClickAbleImageView) findViewById(R.id.btn_broken_traffic);
		checklastUpdate();
		init();

		getLocation();
	}

	void getLocation() {
		gpsTracker = new GPSTracker(MainActivity.this);

		// check if GPS enabled
		if (gpsTracker.canGetLocation()) {

			mLatitude = gpsTracker.getLatitude();
			mLongitude = gpsTracker.getLongitude();

			// \n is for new line
			// Toast.makeText(getApplicationContext(),
			// "Your Location is - \nLat: " + mLatitude + "\nLong: " +
			// mLongitude, Toast.LENGTH_LONG).show();
		} else {
			// can't get location
			// GPS or Network is not enabled
			// Ask user to enable GPS/network in settings
			gpsTracker.showSettingsAlert();
		}
	}

	void init() {

		Log.i("DATA", "LAT: " + mLatitude + " " + "LON: " + mLongitude);
		btnReport.setOnClickListener(this);
		btnView.setOnClickListener(this);

		btnBrokenStreetLight.setOnClickListener(this);
		btnWaterLeak.setOnClickListener(this);
		btnPothole.setOnClickListener(this);
		btnGarbage.setOnClickListener(this);
		btnPowerLine.setOnClickListener(this);
		btnRoadObstruction.setOnClickListener(this);
		btnBrokenTrafficLight.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_report:
			btnReport.setSelected(true);
			btnView.setSelected(false);
			isChoose = true;
			break;
		case R.id.btn_view_report:
			btnReport.setSelected(false);
			btnView.setSelected(true);
			isChoose = false;
			break;

		case R.id.btn_broken_street_light:
			gotoNewAction(1);
			break;
		case R.id.btn_water_leak:
			gotoNewAction(2);
			break;
		case R.id.btn_broken_traffic:
			gotoNewAction(7);
			break;
		case R.id.btn_pothole:
			gotoNewAction(3);
			break;
		case R.id.btn_garbage:
			gotoNewAction(4);
			break;
		case R.id.btn_power_line:
			gotoNewAction(5);
			break;
		case R.id.btn_road_obstruction:
			gotoNewAction(6);
			break;

		default:
			break;
		}
	}

	void gotoReportScreen() {

	}

	public String getValueTitle(int categoryId) {
		if (categoryId == 1) {
			title = "Broken Street Lights";
		} else if (categoryId == 2) {
			title = "Water Leak";
		} else if (categoryId == 3) {
			title = "Pothole";
		} else if (categoryId == 4) {
			title = "Garbage";
		} else if (categoryId == 5) {
			title = "Power line";
		} else if (categoryId == 6) {
			title = "Road obstruction";
		} else if (categoryId == 7) {
			title = "Broken Traffic Light";
		}
		return title;
	}

	void gotoNewAction(int categoryId) {
		if (isChoose == true) {
			Intent t = new Intent(MainActivity.this,
					ReportProblemActivity.class);
			t.putExtra("categoryId", categoryId);
			startActivity(t);
			overridePendingTransition(R.anim.open_next, R.anim.close_main);
		} else {
			Intent t = new Intent(MainActivity.this,
					ViewProblemTestActivity.class);
			t.putExtra("categoryId", categoryId);
			t.putExtra("title", getValueTitle(categoryId));
			t.putExtra("lon", mLongitude);
			t.putExtra("lat", mLatitude);
			startActivity(t);
			overridePendingTransition(R.anim.open_next, R.anim.close_main);
		}

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	public void onLocationChanged(Location location) {
		mLatitude = (float) location.getLatitude();
		mLongitude = (float) location.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	// check last update
	void checklastUpdate() {
		HttpClientHelper httpClientHelper = new HttpClientHelper(
				new MyJsonHttpResponseHandler() {

					@Override
					public void onFailure(Throwable error) {
						super.onFailure(error);
					}

					@Override
					public void onSuccess(JSONObject re) {
						super.onSuccess(re);
						try {
							int status = re.getInt("status");
							if (status == 200) {
								String url = re.getJSONObject("data")
										.getString("logo_url");
								preferenceHelper.setUrlActionbar(url);
								ImageLoader.getInstance().displayImage(url,
										actionbar, options);
							} else {

							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
		httpClientHelper.checkLastUpdate("android");
	}

}
